<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class ArticleController extends BaseController {

    //内容列表
	public function index() {
        $arr = M('article')->select();
        $article_arr = array();
        foreach ($arr as $k => $v) {
            $article_arr[($v['article_type'])][] = $v;
        }
        $cols = array();
        foreach ($article_arr as $k => $v) {
            $cols[$k] = ceil(count($v)/3);
        }
        $this->assign('cols',$cols);
        $this->assign('article_arr',$article_arr);
        $this->display();
    }

    public function showcontent(){
        $data['article_id'] = I('get.id',1);
        $article_arr = M('article')->where($data)->find();
        $article_arr['content'] = file_get_contents('.'.$article_arr['article_url']);
        $this->assign('article_arr',$article_arr);
        $this->display();
    }

    public function article_edit(){
        $data['article_id'] = $_POST['article_id'];
        $article_arr = M('article')->where($data)->find();
        if (empty($article_arr['article_url'])) {
            $filepath = "./Html/article/";
            create_folders($filepath);
            $filename = uniqid().'.html';
            file_put_contents($filepath.$filename,$_POST['content']);
            $_POST['article_url'] = substr($filepath.$filename, 1);
            unset($_POST['content']);
        }else{
            $filepath = '.'.$article_arr['article_url'];
            file_put_contents($filepath , $_POST['content']);
            unset($_POST['content']);
        }
        M('article')->save($_POST);
        $this->success('操作完成','index');
    }

    public function article_add(){
        if ($_POST['action'] == 'article_add_action') {
            unset($_POST['action']);
            if ($_POST != array_filter($_POST)) {
                $this->error('选项不能为空！');
            }
            $filepath = "./Html/article/";
            create_folders($filepath);
            $filename = uniqid().'.html';
            file_put_contents($filepath.$filename,$_POST['content']);
            $_POST['article_url'] = substr($filepath.$filename, 1);
            unset($_POST['content']);
            if (M('article')->add($_POST)) {
                $this->success('操作完成','index');
            }else{
                $this->error('添加内容失败！');
            }
            exit();
        }
        $this->display();
    }


}